<?php

/*
 * This file is part of the FOSUserBundle package.
 *
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Mouta\AmistateBundle\Mailer;

use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\Routing\RouterInterface;
use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Mailer\MailerInterface;

class Mailer implements MailerInterface
{
    protected $mailer;
    protected $router;
    protected $templating;

    public function __construct($mailer, RouterInterface $router, EngineInterface $templating)
    {
        $this->mailer = $mailer;
        $this->router = $router;
        $this->templating = $templating;
    }

    public function sendConfirmationEmailMessage(UserInterface $user)
    {
        $param = $user->getUsername() . '|' .$user->getPassword() . '|edit_perfil';
        $message = \Swift_Message::newInstance();
        $cid = $message->embed(\Swift_Image::fromPath( realpath(__DIR__ . '/../Resources/public/images/amistate.png') ) );
        $message ->setSubject('Bienvenido a Amistate')
            ->setFrom('info@amistate.com','Amistate')
            ->setTo($user->getEmail())
            ->setBody($this->templating->render('AmistateBundle:mails:bienvenida.html.twig', array(
                'param' => base64_encode($param),
                'cid' => $cid,
                'user' =>  $user,
            )),'text/html')
        ;
        $this->mailer->send($message);
    }

    public function sendResettingEmailMessage(UserInterface $user)
    {
        $param = $user->getUsername() . '|' .$user->getPassword() . '|edit_perfil';
        $message = \Swift_Message::newInstance();
        $url = $this->router->generate('fos_user_resetting_reset', array('token' => $user->getConfirmationToken()), true);
        $cid = $message->embed(\Swift_Image::fromPath( realpath(__DIR__ . '/../Resources/public/images/amistate.png') ) );
        $message ->setSubject('Datos de acceso en Amistate')
            ->setFrom('info@amistate.com','Amistate')
            ->setTo($user->getEmail())
            ->setBody($this->templating->render('AmistateBundle:mails:olvidoclave.html.twig', array(
                'param' => base64_encode($param),
                'cid' => $cid,
                'user' =>  $user,
                'url' => $url,
            )),'text/html')
        ;
        $this->mailer->send($message);
    }
}
